﻿using Light;
using UnityEngine;

/// <summary>
///     快捷键打开UI的管理类，需要一直保持活跃状态,不使用分类写法，不适合全部继承Mono类，在原有的UIManager中，存在需要加载场景对象的功能
/// </summary>
public class UIManager_Controller : MonoBehaviour
{
    [SerializeField] private Canvas canvas;
    private bool isMenuOpen;


    private bool isStatusBarOpen;

    private void Start()
    {
    }

    private void Update()
    {
        //开关状态栏
        if (LightInputManager.Instance.StatusOpenCloseInput) StatusBarControl();

        if (LightInputManager.Instance.MenuOpenCloseInput)
        {
            MainMenuControl();
            CloseCurAitvePanel();
        }
    }


    /// <summary>
    ///     关闭当前Active的Panel
    /// </summary>
    private void CloseCurAitvePanel()
    {
        //控制flag变量
        if (isStatusBarOpen)
            isStatusBarOpen = false;
        else if (isMenuOpen) isMenuOpen = false;

        var panel = UIManager.Instance.GetCurActivePanel();

        UIManager.Instance.HidePanel(panel);
    }


    private void StatusBarControl()
    {
        isStatusBarOpen = !isStatusBarOpen; //取得一个相反的状态
        if (isStatusBarOpen)
            OpenStatusBar();
        else
            CloseStatusBar();
    }


    //状态栏相关方法
    private void OpenStatusBar()
    {
        UIManager.Instance.ShowPanel<StatusBarPanel>("StatusBarPanel");
    }

    private void CloseStatusBar()
    {
        UIManager.Instance.HidePanel("StatusBarPanel");
    }

    private void MainMenuControl()
    {
        var panel = UIManager.Instance.GetCurActivePanel();

        //只有当前没有UIManager控制的面板活跃
        if (panel == null)
        {
            isMenuOpen = !isMenuOpen;
            if (isMenuOpen)
                OpenMenu();
            else
                CloseMenu();
        }
    }

    private void OpenMenu()
    {
        UIManager.Instance.EnterUI();
        canvas.gameObject.SetActive(true);
    }

    public void CloseMenu()
    {
        UIManager.Instance.ExitUI();
        canvas.gameObject.SetActive(false);
    }
}